<html>
<head><meta charset="utf-8"><title>Testing whether a type implements a trait with assoc type · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Testing.20whether.20a.20type.20implements.20a.20trait.20with.20assoc.20type.html">Testing whether a type implements a trait with assoc type</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226321850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Testing%20whether%20a%20type%20implements%20a%20trait%20with%20assoc%20type/near/226321850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boolean_coercion <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Testing.20whether.20a.20type.20implements.20a.20trait.20with.20assoc.20type.html#226321850">(Feb 14 2021 at 19:58)</a>:</h4>
<p>How do I test whether a type implements a trait with a specific associated type, similar to having the bound <code>Trait&lt;Type = X&gt;</code>? For context, I'm trying to check whether some type implements <code>Deref&lt;Target = str&gt;</code>.</p>
<p>I found <code>clippy_lints::utils::implements_trait</code>, but that only takes generic type parameters.</p>



<a name="226421232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Testing%20whether%20a%20type%20implements%20a%20trait%20with%20assoc%20type/near/226421232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Testing.20whether.20a.20type.20implements.20a.20trait.20with.20assoc.20type.html#226421232">(Feb 15 2021 at 19:16)</a>:</h4>
<p>Hmm. I think the associated type is not needed when asking "does X implement Y".</p>



<a name="226421705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Testing%20whether%20a%20type%20implements%20a%20trait%20with%20assoc%20type/near/226421705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Testing.20whether.20a.20type.20implements.20a.20trait.20with.20assoc.20type.html#226421705">(Feb 15 2021 at 19:21)</a>:</h4>
<p>Do you need to ask, "If X implements Deref, then what is Target for that Impl?" I'm not sure how to do that. Maybe you should explain what you need.</p>



<a name="226424180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Testing%20whether%20a%20type%20implements%20a%20trait%20with%20assoc%20type/near/226424180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boolean_coercion <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Testing.20whether.20a.20type.20implements.20a.20trait.20with.20assoc.20type.html#226424180">(Feb 15 2021 at 19:49)</a>:</h4>
<p>My problem is related to this <a href="https://github.com/rust-lang/rust-clippy/pull/6717#pullrequestreview-589891735">issue</a>. I'd like to know when I'm able to remove the <code>&amp;</code> operator, but that's only possibly if I know that the type being referenced implements <code>Deref&lt;Target = str&gt;</code>. You could check manually if the type is <code>String</code> or <code>&amp;str</code> of course, but I'd like to be the most general so that this will work with any user-defined type as well.</p>



<a name="226696106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Testing%20whether%20a%20type%20implements%20a%20trait%20with%20assoc%20type/near/226696106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Testing.20whether.20a.20type.20implements.20a.20trait.20with.20assoc.20type.html#226696106">(Feb 17 2021 at 17:45)</a>:</h4>
<p><span class="user-mention" data-user-id="372858">@boolean_coercion</span> I think <code>expr_adjustments</code> would fit your need. It reveals implicit derefs.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>